package cn.edu.neu.studentmsg_java.controller.workbench;

import cn.edu.neu.studentmsg_java.cons.R;
import cn.edu.neu.studentmsg_java.cons.State;
import cn.edu.neu.studentmsg_java.model.Course;
import cn.edu.neu.studentmsg_java.model.Student;
import cn.edu.neu.studentmsg_java.service.CourseService;
import cn.edu.neu.studentmsg_java.service.MCService;
import cn.edu.neu.studentmsg_java.service.MajorService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/workbench/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private MCService mcService;

    //获取专业名称
    @RequestMapping("/getMajorData")
    public R getMajorData() {
        //查询专业名称
        List<String> majorNameList = majorService.findMajorName();
        if (ObjectUtils.isNotEmpty(majorNameList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(majorNameList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //获取课程名称
    @RequestMapping("/getCourseData")
    public R getCourseData() {
        //查询课程名称
        List<String> courseNameList = courseService.findCourseName();
        if (ObjectUtils.isNotEmpty(courseNameList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(courseNameList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //根据任意条件进行课程查询
    @RequestMapping("/findCoursePart")
    public R findCoursePartByStudentId(Course course) {
        //动态查询课程信息
        List<Course> MajorCourseList = courseService.findCoursePart(course);
        if (ObjectUtils.isNotEmpty(MajorCourseList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(MajorCourseList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过课程编号删除课程信息，并删除与他相关联的信息
    @RequestMapping("/removeCourseByCourseId")
    public R removeCourseByCourseId(Integer course_id) {
        //通过课程id去删除课程信息
        int result = courseService.removeCourseById(course_id);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_DELETE_ERROR.getCode()).msg(State.DB_DELETE_ERROR.getMsg()).build();
    }

    //添加课程信息，如果为主修课，则通过专业名称会绑定一个专业上
    @RequestMapping("/addCourse")
    public R addCourse(@RequestBody Course course) {
        //添加课程信息
        int result = courseService.addCourse(course);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_SAVE_ERROR.getCode()).msg(State.DB_SAVE_ERROR.getMsg()).build();
    }

    //通过课程id获取课程修改页面所需要的专业名称
    @RequestMapping("/toModifyCoursePage")
    public R toModifyCoursePage(Integer course_id) {
        //通过课程id查询课程
        Course course = mcService.findCourseByCourseId(course_id);
        //判断查询出的课程不为空
        if (ObjectUtils.isNotEmpty(course)) {
            //通过课程id获取专业名称并封装在course中
            course.setMajor_name(courseService.findMajorNameByCourseId(course_id));
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(course).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();

    }

    //通过课程编号，修改课程相关信息
    @RequestMapping("/modifyCourseById")
    public R modifyCourseById(@RequestBody Course course) {
        //通过课程id去修改课程信息
        int result = courseService.modifyCourseById(course);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_UPDATE_ERROR.getCode()).msg(State.DB_UPDATE_ERROR.getMsg()).build();
    }

}
